package com.ag.lzw.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.ag.bean.TbTask;
import com.ag.dao.TbTaskDAO;
import com.ag.lzw.service.impl.HandelTasksImpl;

/**
 * @author zhiweiofli
 * TbTaskDAO的子类
 * */
public class LzwTaskDAO extends TbTaskDAO {

	private static final Logger logger = Logger.getLogger(LzwTaskDAO.class);
	/**
	 * 分页查询
	 * 
	 * @param sql
	 *            String
	 * @param firstRow
	 *            int
	 * @param maxRow
	 *            int
	 * @return List
	 */
	public List findByPage(final String hql,final int pageNo, final int pageSize){
		List findRes = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws SQLException,
					HibernateException {
				Query q = session.createQuery(hql);
				q.setFirstResult(pageNo);
				q.setMaxResults(pageSize);
				return q.list();
			}
		});
		return findRes;
	}
	
	/**@return 任务的总数 
	 *                 int
	 * */
	public int getCountofTask(String hql){
        return getHibernateTemplate().find(hql).size();
    }
	
	/**
	 * 根据任务ID查询任务，并抓取其属下的子任务
	 * @param id
	 *           String
	 * @return 任务实体的集合
	 *         List
	 */
	public List findTasksById(String id){
		String hql = "From TbTask tt " +
		        "left join fetch tt.tbUserDept "+
				"left join fetch tt.tbTask " +
				"left join fetch tt.tbTasks " +
				"left join fetch tt.tbTaskFeedbacks " +
				"where tt.TId='"+id+"'";
		return getHibernateTemplate().find(hql);
	}
	/**
	 * 根据事件的ID查询属于该事件的根任务
	 * @param Eid
	 *            String
	 * @return 任务实体的集合
	 *         List
	 */
	public List<TbTask> findTaskByEid(String Eid){
		String hql = "From TbTask tt " +
				"left join fetch tt.tbTask " +
				"left join fetch tt.tbUserDept "+
				"where tt.tbTask.TId = null and tt.tbEvent.EId='"+Eid+"'";
		return getHibernateTemplate().find(hql);
	}
	
	/**
	 * 由任务ID查找一个任务的实体
	 * @param id
	 * @return TbTask
	 */
	public TbTask findATaskById(String id){
		
		String hql = "From TbTask tt where tt.TId='"+id+"'";  //用？参数注入时，如果id的位数不足容易出现问题，用拼hql语句的方式可以避免
		List<TbTask> list = getHibernateTemplate().find(hql);  
		TbTask result = null;
		logger.info("Result list length:"+list.size());
		for(TbTask tt : list){
			if(tt!=null){
				logger.info("get a task,id:"+tt.getTId());
				result = tt;
			}
		}
		return result;
	}
	/**
	 * 根据用户ID查询属于其的任务，同时抓取属于该任务实体的部分属性
	 * @param uid
	 * @return
	 */
	public List<TbTask> findTasksofUser(String uid){
		String hql = "From TbTask as tt " +
				     "left join fetch tt.tbEvent " +
				     "left join fetch tt.tbUserDept " +
				     "left join fetch tt.tbUser " +
				     "left join fetch tt.tbTaskStatus where tt.tbUser.UId='"+uid+"'";
		List<TbTask> res = (List<TbTask>)getHibernateTemplate().find(hql);
		return res;
	}
	/**
	 * 根据任务ID查询属于其的任务，同时抓取属于该任务实体的部分属性
	 * @param tid
	 * @return
	 */
	public TbTask findTaskDetailById(String tid){
		String hql = "From TbTask as tt left join fetch tt.tbEvent " +
		"left join fetch tt.tbUserDept " +
		"left join fetch tt.tbUserDept.tbUser "+
		"left join fetch tt.tbUser " +
		"left join fetch tt.tbTaskStatus where tt.TId='"+tid+"'";
		List<TbTask> list = (List<TbTask>)getHibernateTemplate().find(hql);
		TbTask result = null;
		logger.info("Task list length:"+list.size());
		for(TbTask tt : list){
			if(tt!=null){
				logger.info("get a task,id:"+tt.getTId());
				result = tt;
			}
		}
		return result;
	}
	/**
	 * 根据分派人ID,查询任务
	 * @return
	 */
	public List findTasksByB(String uid){
		String hql = "from TbTask as tt where tt.TId='"+uid+"'";
		return this.getHibernateTemplate().find(hql);
	}
}
